package com.xinchuang.initializrdemo.controller;

import cn.hutool.core.util.StrUtil;
import com.xinchuang.initializrdemo.entity.User;
import com.xinchuang.initializrdemo.service.UserService;
import com.xinchuang.initializrdemo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 按类型注入
     */
    @Autowired
    private UserService userService;


    /**
     * 查询
     */
    @GetMapping("/userList")
    public R userList(){
        List<User> userList = userService.getUserList();
        return R.data(userList);
    }

    /**
     * 添加
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public R addUser(@RequestBody User user){
        // 校验参数
        if(StrUtil.isEmpty(user.getName())){
            R.error("用户名称不能为空");
        }
        String userId = userService.addUser(user);
        return R.data(userId);
    }

    @PutMapping("/editUser")
    public R editUser(@RequestBody User user){
        // 校验参数
        if(StrUtil.isEmpty(user.getName())){
            R.error("用户名称不能为空");
        }
        String msg = userService.editUser(user);
        return R.ok(msg);
    }
    @DeleteMapping("/deleteUser/{userId}")
    public R deleteUser(@PathVariable String userId){
        String msg = userService.deleteUser(userId);
        return R.ok(msg);
    }
    @GetMapping("/deleteUserByGet")
    public R deleteUserByGet(@RequestParam String userId){
        String msg = userService.deleteUser(userId);
        return R.ok(msg);
    }
}
